class Solution {
public:
    /**
     * @param str: the string
     * @return: the number of substrings 
     */
    int stringCount(string &str) {
        // Write your code here.
        int len = 0;
        int num_of_substr = 0;
        
        for (auto it = str.begin(); it != str.end(); it++) {
            if (*it == '0') {
                len++;
            } else {
                num_of_substr += len * (len + 1) / 2;
                len = 0;
            }
        }
        
        if (len > 0) {
            num_of_substr += len * (len + 1) / 2;
        }
        
        return num_of_substr;
    }
};

class Solution {
public:
    /**
     * @param str: the string
     * @return: the number of substrings 
     */
    int stringCount(string &str) {
        // Write your code here.
        size_t len = str.size();
        int num_of_substr = 0;
        
        size_t start = 0;
        size_t end = 0;
      
       do {
           /* find first '0' character */
           while (start < len && str[start] != '0') {
               start++;
           }
           
           /* if we reach the end of string, stop it */
           if (start == len) {
               break;
           }
           
           end = start;
           /* then we find the continous '0' substring */
           while (end < len && str[end] == '0') {
               end++;
           }
           
           num_of_substr += (end - start) * (end - start + 1) / 2;
           
           /* if we reach the end of string, stop it */
           if (end == len) {
               break;
           }
           
           start = end;
       } while (1);
       
       return num_of_substr;
    }
};
